/**
 * Implementation of main data structure
 *
 * author: Marcin Wachulski
 *
 */
#include "DataStructure.h"

DataStructure::DataStructure() {
    this->_allAttrs = new AttributeSet();
    this->_allObjs = new ObjectSet();
}
DataStructure::~DataStructure() {
    // cleaning attributes with object references
    for (std::map<Attribute, ObjectSet*>::iterator i = this->_attrs.begin();
        i != this->_attrs.end(); i++)
        delete i->second;
    // cleaning objects with attribute references
    for (std::vector<AttributeSet*>::iterator i = this->_objs.begin();
        i != this->_objs.end(); i++)
        delete *i;
    // cleaning the full sets
    delete this->_allAttrs;
    delete this->_allObjs;
}

void DataStructure::updateAttribute(const Attribute &attr, const Object &obj) {
    ObjectSet * &objSet = this->_attrs[attr];
    if (objSet == NULL)
        objSet = new ObjectSet();
    objSet->data.insert(obj);
    // update complete AllAttributes set
    this->_allAttrs->data.insert(attr);
}

// object is not used since object vector is assumend to be a completed list of
// objects (a sequence of integers) ordered asc - performance optimization
void DataStructure::addObject(const Object &obj, AttributeSet *aSet) {
    this->_objs.push_back(aSet);
    this->_allObjs->data.insert(obj);
}

// summarize the structure
void DataStructure::printSummary(std::ostream &out) {
    // basic properties
    out << "Total attributes: " << this->getAllAtributes()->data.size() << std::endl;
    out << "Total objects: " << this->getAllObjects()->data.size() << std::endl;

    // counting relation elements
    int r_elem = 0;
    for (std::vector<AttributeSet*>::const_iterator i = this->_objs.begin();
        i != this->_objs.end(); i++)
        r_elem += (*i)->data.size();
    out << "Total relation elements: " << r_elem;
}